<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/1/12
 * Time: 8:58 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
jimport('joomla.html.pagination');

/**
 * Congtacs Model
 */
class VastModelCongtacs extends JModelItem {
    protected $congtacs;
    private $_total = null;
    private $_pagination = null;

    public function __construct()
    {
        parent::__construct();
        
        $config = JVastConfig::getConfig();
        
        $this->setState('limit', (isset($config['numOfItemsPerPage']) ? (int)$config['numOfItemsPerPage'] : JRequest::getVar('limit', 5, '', 'int')));
        $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
        $session = JFactory::getSession();

        $ten = JRequest::getString('canboTen', $session->get('congtacs.ten'));
        $phong = JRequest::getInt('congtacsPhong', $session->get('congtacs.phong'));
        $donvi = JRequest::getInt('congtacsDonvi', $session->get('congtacs.donvi'));  
              
        $session->set('congtacs.ten', $ten);
        $session->set('congtacs.phong', $phong);
        $session->set('congtacs.donvi', $donvi);
                
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
            
        $query->select('a.*, b.hoten, c.ten AS tenphong, d.ten AS tendonvi, e.mota');
        $query->from('#__congtac AS a');
        $query->join('INNER', '#__hosocanbo AS b ON b.id = a.hosocanbo_id');
        $query->join('LEFT', '#__phong AS c ON c.id = a.phong_id');
        $query->join('LEFT', '#__donvi AS d ON d.id = a.donvi_id');        
        $query->join('INNER', '#__tinhtranglamviec AS e ON e.id = a.tinhtranglamviec_id');
        
        if (!is_null($phong) && $phong != -1) {
            $query->where('a.phong_id = ' . (int)$phong);
        }
           
        if (!is_null($donvi) && $donvi != -1) {
            $query->where('a.donvi_id = ' . (int)$donvi);
        }

        if (!is_null($ten) && $ten !== '') 
            $query->where('b.hoten like \'%' . $ten . '%\'');
        elseif ($ten === '')
            $query->where('b.hoten like \'%\'');
                
        $db->setQuery($query);
        
        $this->congtacs = $db->loadObjectList();
            
        $this->_total = count($this->congtacs);          
    }

    /**
     * Get the message
     * @return string The message to be displayed to the user
     */
    public function getCongtacs()
    {
        $session = JFactory::getSession();
        $ten = $session->get('congtacs.ten');
        $phong = $session->get('congtacs.phong');
        $donvi = $session->get('congtacs.donvi');
        
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
            
        $limit = $this->getState('limit');
        $limitstart = $this->getState('limitstart');

        $query->select('a.*, b.hoten, c.ten AS tenphong, d.ten AS tendonvi, e.mota');
        $query->from('#__congtac AS a');
        $query->join('INNER', '#__hosocanbo AS b ON b.id = a.hosocanbo_id');
        $query->join('LEFT', '#__phong AS c ON c.id = a.phong_id');
        $query->join('LEFT', '#__donvi AS d ON d.id = a.donvi_id');        
        $query->join('INNER', '#__tinhtranglamviec AS e ON e.id = a.tinhtranglamviec_id');
        
        if (!is_null($phong) && $phong != -1) {
            $query->where('a.phong_id = ' . (int)$phong);
        }

        if (!is_null($donvi) && $donvi != -1) {
            $query->where('a.donvi_id = ' . (int)$donvi);
        }
           
        if (!is_null($ten) && $ten !== '') 
            $query->where('b.hoten like \'%' . $ten . '%\'' . ' LIMIT ' . $limitstart . ',' . $limit);
        elseif ($ten === '')
            $query->where('b.hoten like \'%\'' . ' LIMIT ' . $limitstart . ',' . $limit);
                
        $db->setQuery($query);
        
        $this->congtacs = $db->loadObjectList();
                            
        return $this->congtacs;
    }
    
    public function getChiTietCongTac()
    {
        $id = JRequest::getInt('congtacid');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*, b.hoten, c.ten AS tendonvi, d.ten AS tenphong, e.mota');
        $query->from('#__congtac AS a');
        $query->join('INNER', '#__hosocanbo AS b ON a.hosocanbo_id = b.id');
        $query->join('LEFT', '#__donvi AS c ON c.id = a.donvi_id');
        $query->join('LEFT', '#__phong AS d ON d.id = a.phong_id');
        $query->join('INNER', '#__tinhtranglamviec AS e ON e.id = a.tinhtranglamviec_id');
        $query->where('a.id = ' . $id);

        $db->setQuery($query);
        $congtac = $db->loadObject();
        
        return $congtac;        
    }
    
    public function getPagination()
    {
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        return $this->_pagination;
    }

    public function getTotal()
    {
        return $this->_total;
    }  
    
    public function getPhong()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
                
        $query->select('a.*');
        $query->from('#__phong AS a');

        $db->setQuery($query);
        $phongs = $db->loadObjectList();

        return $phongs;        
    }

    public function getDonvi()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
                
        $query->select('a.*');
        $query->from('#__donvi AS a');

        $db->setQuery($query);
        $phongs = $db->loadObjectList();

        return $phongs;        
    }

    /**
     * Get the hosocanbo
     * @return string The message to be displayed to the user
     */
    public function getCanbo()
    {
        $id = JRequest::getInt('congtacid');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__hosocanbo AS a');
        $query->join('INNER', '#__congtac AS b ON b.hosocanbo_id = a.id');
        $query->where('b.id = ' . $id);

        $db->setQuery($query);
        $canbo = $db->loadObject();
        return $canbo;
    }  
    
}